package org.wit.ecommerce.service.impl;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.wit.ecommerce.dao.TransactionDao;
import org.wit.ecommerce.domain.Transaction;
import org.wit.ecommerce.domain.TransactionLog;
import org.wit.ecommerce.dto.Navigator;
import org.wit.ecommerce.service.ITransactionService;
import org.wit.ecommerce.util.PageNavigateCalculator;
/**
 * @spring.bean id="transactionService" parent="baseTransactionProxy" extends="true" transactionProxy="true"
 * @author Justin
 *
 */
public class TransactionServiceImpl implements ITransactionService {
	private TransactionDao transactionDao;
	
	@Override
	public void createTransaction(Transaction transaction) {
		transactionDao.save(transaction);
	}

	@Override
	public Transaction getTransactionById(Long id) {
		Transaction t = transactionDao.get(id);
		Hibernate.initialize(t.getTransactionLogs());
		return t;
	}

	@Override
	public Navigator<Transaction> getTransactionsByKeywords(int page, Map<String, ?> keywords) {
		PageNavigateCalculator pnc = new PageNavigateCalculator();
		pnc.setCurrentPage(page);
		pnc.setTotalRowsAmount(transactionDao.getTotalCount(keywords));
		int start = pnc.getCurrentPageRows();
		int limit = pnc.getPageSize();
		
		List<Transaction> transactions = transactionDao.findByPage(start, limit, keywords);
		Navigator<Transaction> navigator = new Navigator<Transaction>();
		navigator.setData(transactions);
		navigator.setNext(pnc.HasNext());
		navigator.setPageSize(pnc.getPageSize());
		navigator.setPrevious(pnc.HasPrevious());
		navigator.setTotalPages(pnc.getTotalPages());
		navigator.setTotalRowsAmount(pnc.getTotalRowsAmount());
		return navigator;
	}

	@Override
	public Transaction getTransactionByPin(String pin) {
		Transaction t = transactionDao.findUniqueBy("pin", pin);
		Hibernate.initialize(t.getTransactionLogs());
		return t;
	}

	@Override
	public void updateTransaction(Transaction transaction) {
		transactionDao.save(transaction);
	}

	@Override
	public void changeTransactionStatus(Long id, int status) {
		Transaction transaction = transactionDao.get(id);
		transaction.setStatus(status);
		
		TransactionLog log = new TransactionLog();
		log.setCreateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
		switch(status){
		case Transaction.CANCELLED: 
			log.setLog("CANCELLED"); break;
		case Transaction.UNPAID: 
			log.setLog("UNPAID"); break;
		case Transaction.PAID: 
			log.setLog("PAID"); break;
		case Transaction.SENT: 
			log.setLog("SENT"); break;
		case Transaction.RECEIVED: 
			log.setLog("RECEIVED"); break;
		default: log.setLog("UNKNOWN");
		}
		log.setTransaction(transaction);
		
		transaction.getTransactionLogs().add(log);
		
		transactionDao.save(transaction);
	}

	@Override
	public void deleteTransactionsByIds(Long[] ids) {
		transactionDao.batchDelete(ids);
	}

	@Override
	public List<TransactionLog> getTransactionLogs(Long id) {
		Transaction transaction = transactionDao.get(id);
		return transaction.getTransactionLogs();
	}

	/**
	 * @spring.property ref="transactionDao"
	 * @param transactionDao
	 */
	public void setTransactionDao(TransactionDao transactionDao) {
		this.transactionDao = transactionDao;
	}
}
